package pt.inescid.components.policyengine.heimdall.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.base.Config;
import pt.inescid.components.policyengine.heimdall.commons.DBManager;
import pt.inescid.components.policyengine.heimdall.commons.Log;

public class ConfigDAO {
	
	private static Vector configTags = null;

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= getRecordByTag =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static Config getRecordByTag(String tag) {
		
		ResultSet rs = null;
		Config currentRecord = null;
		
		if (configTags == null) {
			updateCache();
		}
		
		for (int i = 0; i < configTags.size(); i++) {
			currentRecord = (Config) configTags.elementAt(i);
			if (currentRecord.getTag().equals(tag)) {
				return (currentRecord);
			}
		}
		
		return (null);
	}; // getRecord
	
	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 ============= updateCache =====================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	public static void updateCache() {
		
		ResultSet rs = null;
		Config currentRecord = null;
		
		configTags = new Vector();
		
		String query = "SELECT * FROM config;";
		
		try {
			rs = DBManager.executeQuery(query);
			
			if (rs.first()) {
				do {
					currentRecord = createRecord(rs);
					configTags.add(currentRecord);
				} while (rs.next());
			}
			
		}
		catch (SQLException e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e + 
					" - query = " + query, "ConfigDAO.updateCache");
			currentRecord = null;
		}
	}; // updateCache
	

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      createRecord   =============================
	 =============                    =============================
	 ==============================================================
	 =========================================================== */
	
	public static Config createRecord(ResultSet rs) throws SQLException {
		Config newRecord = null;
		
		try {
			newRecord = new Config(
					rs.getString("param"),
					rs.getString("value"));
		}
		catch (Exception e) {
			Log.debug(Log.ERROR, "Erro SQL: " + e, "ConfigDAO.createRecord");
			newRecord = null;
		}
		
		return (newRecord);
	}; // createRecord
	

}; // ConfigDAO
